//
//  WPCPreviewView.m
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/6.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

#import "WPCPreviewView.h"
#import "UIImageView+WebCache.h"
#import "WPCLayout.h"
#import "WPCThemesInfo.h"

@interface WPCPreviewView()
@property(nonatomic, strong)    UIImageView *picImage;
@property(nonatomic, strong)    UIButton    *shareBtn;
@property(nonatomic, strong)    UIButton    *downloadBtn;
@property(nonatomic, strong)    UIButton    *favoriteBtn;
@property(nonatomic, strong)    UIImageView *shadowShare;
@property(nonatomic, strong)    UIImageView *shadowDownload;
@property(nonatomic, strong)    UIImageView *shadowFavorite;
@property(nonatomic, strong)    UILabel     *titleLabel;

@property(nonatomic, strong)    MBProgressHUD   *hud;
@end

@implementation WPCPreviewView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect imgFrame = self.bounds;
        imgFrame.size.height *= 0.8;
        _picImage = [[UIImageView alloc] initWithFrame:imgFrame];
        [self addSubview:_picImage];
        
        _hud = [[MBProgressHUD alloc] init];
        [self addSubview:_hud];
        
        _shadowShare = [[UIImageView alloc] init];
        _shadowShare.backgroundColor = [UIColor whiteColor];
        _shadowShare.userInteractionEnabled = YES;
        
        _shadowDownload = [[UIImageView alloc] init];
        _shadowDownload.backgroundColor = [UIColor whiteColor];
        _shadowDownload.userInteractionEnabled = YES;
        
        _shadowFavorite = [[UIImageView alloc] init];
        _shadowFavorite.backgroundColor = [UIColor whiteColor];
        _shadowFavorite.userInteractionEnabled = YES;
        
        [self addSubview:_shadowShare];
        [self addSubview:_shadowDownload];
        [self addSubview:_shadowFavorite];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont fontWithName:@"Helvetica" size:10];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        CREATE_UIButton(_shareBtn, _shadowShare, @"share", BTN_SHARE);
        _shadowShare.tag = BTN_SHARE;
        CREATE_UIButton(_downloadBtn, _shadowDownload, @"download", BTN_DOWNLOAD);
        _shadowDownload.tag = BTN_DOWNLOAD;
        CREATE_UIButton(_favoriteBtn, _shadowFavorite, @"love", BTN_FAVORITE);
        _shadowFavorite.tag = BTN_FAVORITE;
        
        [self layoutWithMasonry];
    }
    
    return self;
}

- (void)setThumbnailURL:(NSURL*)thumbnailURL originalPicURL:(NSURL*)originalPicURL
{
    [_picImage sd_setImageWithURL:originalPicURL placeholderImage:[self imageFromCacheWithURL:thumbnailURL]];
}

- (void)setTitle:(NSString*)title
{
    [_titleLabel setText:title];
}

- (void)setIsFavorite:(BOOL)isFavorite
{
    NSString *picName = isFavorite ? @"love2" : @"love";
    [_favoriteBtn setImage:[UIImage imageNamed:picName] forState:UIControlStateNormal];
}

#pragma mark <event>
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (BTN_SHARE == touch.view.tag) {
        [_delegate doShare:nil];
    }
    else if (BTN_DOWNLOAD == touch.view.tag) {
        [_delegate doDownload:nil];
    }
    else if (BTN_FAVORITE == touch.view.tag) {
        [_delegate doFavorite:^(WPCThemesItem *item) {
            [self setIsFavorite:item.isFavorite.boolValue];
            [self showFavoriteToast:item.isFavorite.boolValue];
        }];
    }
}

#pragma mark <private method>
-(UIImage*)imageFromCacheWithURL:(NSURL*)URL
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    NSString* key = [manager cacheKeyForURL:URL];
    SDImageCache* cache = [SDImageCache sharedImageCache];
    //此方法会先从memory中取。
    UIImage *image = [cache imageFromDiskCacheForKey:key];
    return image;
}

//布局约束
- (void)layoutWithMasonry
{
    self.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5f];
    
    [_shadowShare makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_picImage);
        make.top.equalTo(_picImage.bottom);
        make.width.equalTo(_picImage.width).offset(-0.5).multipliedBy(1.0/3.0);
        make.height.equalTo(self.bounds.size.height * 0.2 * 0.6);
    }];
    
    [_shadowDownload makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_shadowShare.trailing).offset(0.5);
        make.top.equalTo(_shadowShare.top);
        make.size.equalTo(_shadowShare);
    }];
    
    [_shadowFavorite makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_shadowDownload.trailing).offset(0.5);
        make.top.equalTo(_shadowShare.top);
        make.size.equalTo(_shadowShare);
    }];
    
    [_shareBtn makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_shadowShare);
        make.size.equalTo(CGSizeMake(50, 50));
    }];
    
    [_downloadBtn makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_shadowDownload);
        make.size.equalTo(_shareBtn);
    }];
    
    [_favoriteBtn makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_shadowFavorite);
        make.size.equalTo(_shareBtn);
    }];
    
    [_titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_picImage);
        make.top.equalTo(_shadowShare.bottom).offset(0.5);
        make.width.equalTo(_picImage);
        make.bottom.equalTo(self.bottom);
    }];
    
    [_hud makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_picImage);
    }];
}

- (void)showFavoriteToast:(BOOL)isFavorite
{
    _hud.mode = MBProgressHUDModeText;
    
    _hud.label.text = isFavorite ? @"love this paper" : @"not my favorite";
    [_hud showAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_hud hideAnimated:YES];
    });
}


@end
